package com.example.thegreatwall.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.thegreatwall.entity.TProduct;
import com.example.thegreatwall.mapper.TProductMapper;
import com.example.thegreatwall.service.TProductService;
import org.springframework.stereotype.Service;

/**
 * 套餐商品服务实现类
 */
@Service
public class TProductServiceImpl extends ServiceImpl<TProductMapper, TProduct> implements TProductService {

    @Override
    public Page<TProduct> listTProducts(Integer pageNum, Integer pageSize, Integer packageType) {
        Page<TProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TProduct> wrapper = new LambdaQueryWrapper<>();
        
        // 如果指定了套餐类型，则按类型筛选
        if (packageType != null) {
            wrapper.eq(TProduct::getPackageType, packageType);
        }
        
        // 只查询正常状态的套餐
        wrapper.eq(TProduct::getStatus, 1)
                .orderByDesc(TProduct::getHotLevel)
                .orderByDesc(TProduct::getCreateTime);
        
        return page(page, wrapper);
    }

    @Override
    public TProduct getTProductDetail(Integer tProductId) {
        return getById(tProductId);
    }

    @Override
    public Page<TProduct> getTProductsByType(Integer packageType, Integer pageNum, Integer pageSize) {
        Page<TProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TProduct> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.eq(TProduct::getPackageType, packageType)
                .eq(TProduct::getStatus, 1)
                .orderByDesc(TProduct::getHotLevel)
                .orderByDesc(TProduct::getCreateTime);
        
        return page(page, wrapper);
    }

    @Override
    public Page<TProduct> searchTProducts(String keyword, Integer pageNum, Integer pageSize) {
        Page<TProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TProduct> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.and(w -> w.like(TProduct::getName, keyword)
                        .or()
                        .like(TProduct::getDescription, keyword))
                .eq(TProduct::getStatus, 1)
                .orderByDesc(TProduct::getHotLevel)
                .orderByDesc(TProduct::getCreateTime);
        
        return page(page, wrapper);
    }

    @Override
    public Page<TProduct> getHotTProducts(Integer pageNum, Integer pageSize) {
        Page<TProduct> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<TProduct> wrapper = new LambdaQueryWrapper<>();
        
        wrapper.eq(TProduct::getStatus, 1)
                .gt(TProduct::getHotLevel, 0)  // 热门等级大于0
                .orderByDesc(TProduct::getHotLevel)
                .orderByDesc(TProduct::getScore)
                .orderByDesc(TProduct::getCreateTime);
        
        return page(page, wrapper);
    }
}
